#include "FrakOutMigrationController.h"
#include "FrakOutVersion.h"
#include "FrakOutVersionState.h"
#include "FrakOutVersionStateFactory.h"

FrakOutMigrationController::FrakOutMigrationController()
{
    m_states.push_back(FrakOutVersionStateFactory::create(FrakOutVersion::V1_0_0()));
    m_states.push_back(FrakOutVersionStateFactory::create(FrakOutVersion::V1_1_0()));
}

FrakOutMigrationController::~FrakOutMigrationController()
{
    foreach(FrakOutVersionState *state, m_states)
        delete state;
}

void FrakOutMigrationController::migrate(FrakOutData& frakOutData,
                                         const FrakOutVersion* const startVersion,
                                         const FrakOutVersion* const endVersion)
{
    QList<FrakOutVersionState*>::const_iterator beginIt =
            std::find_if(m_states.begin(), m_states.end(), FrakOutVersionState::MatchVersion(startVersion));
    QList<FrakOutVersionState*>::const_iterator endIt =
            std::find_if(m_states.begin(), m_states.end(), FrakOutVersionState::MatchVersion(endVersion));
    QList<FrakOutVersionState*>::const_iterator stateIt;

    for (stateIt = beginIt; stateIt != endIt; stateIt++)
        (*stateIt)->migrate(frakOutData);
}
